using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Data;
using Deepcode.Flux.Core.Services.Configuration;
using Deepcode.Flux.Core.Systems.Workflow.Bases;
using Deepcode.Flux.Core.Systems.Workflow.Enums;
using Deepcode.Flux.Core.Systems.Workflow.Info;
using Deepcode.Flux.Core.Systems.Workflow;
using Deepcode.Flux.Core.Services.State;
using Deepcode.Flux.Core.Extensions;
using Deepcode.Flux.Core.Systems.Audit.Quick;
using Deepcode.Flux.Core.Services.Assistant;

public partial class Private_Config_Workflow_ui_newexitpath : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region Permission Code Override
	public override string RequiredPermissionCode { get { return "FluxWorkflowEdit"; } }
	#endregion
	#region ViewState storage
	/// <summary>
	/// Stores the ID of the handler which this form
	/// is adding a new definition to.
	/// </summary>
	public string HandlerID
	{
		get
		{
			object o = ViewState["HandlerID"];
			if (o == null) return String.Empty;
			return (string)o;
		}
		set
		{
			ViewState["HandlerID"] = value;
		}
	}
	/// <summary>
	/// Stores the ID of the definition being edited
	/// </summary>
	public int DefinitionID
	{
		get
		{
			object o = ViewState["DefinitionID"];
			if (o == null) return -1;
			return (int)o;
		}
		set
		{
			ViewState["DefinitionID"] = value;
		}
	}
	/// <summary>
	/// Stores the ID of the process being edited
	/// </summary>
	public int ProcessID
	{
		get
		{
			object o = ViewState["ProcessID"];
			if (o == null) return -1;
			return (int)o;
		}
		set
		{
			ViewState["ProcessID"] = value;
		}
	}
	/// <summary>
	/// Stores the ID of the tasking being edited
	/// </summary>
	public int TaskingID
	{
		get
		{
			object o = ViewState["TaskingID"];
			if (o == null) return -1;
			return (int)o;
		}
		set
		{
			ViewState["TaskingID"] = value;
		}
	}
	#endregion
	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			// Store any state information from the calling listing
			this.HandlerID = GetAttribute("HandlerID", "");
			this.DefinitionID = GetAttribute("DefinitionID", -1);
			this.ProcessID = GetAttribute("ProcessID", -1);
			this.TaskingID = GetAttribute("TaskingID", -1);

			// Load info 
			Handler h = new Handlers().GetHandlerByID( this.HandlerID );
			if( h == null ) GoBack();
			Definition d = new Definitions().GetByID( this.DefinitionID );
			if( d == null ) GoBack();
			Process p = new Processes().GetProcessByID( this.ProcessID );
			if( p == null ) GoBack();
			Tasking t = new Taskings().GetTaskingByID( this.TaskingID );
			if( t == null ) GoBack();
			
			// Display infos
			labHandler.Text = h.Name;
			labDefinition.Text = d.DefName;
			labProcess.Text = p.ProcessName;
			labTasking.Text = t.TaskCaption;

			// Create an engine
			WorkflowEngine Engine = new WorkflowEngine(this.HandlerID);
			// Populate list of UI Methods available
			uiTaskMethod.Items.Clear();
			uiTaskMethod.Items.Add( new ListItem( "(none)", "" ));
			foreach( WorkflowUIMethodInfo ui in Engine.GetWorkflowUIMethods() )
				uiTaskMethod.Items.Add( new ListItem( ui.FriendlyName, ui.MethodName ));

			// Populate list of groups available
			CSV_AllowedGroups.Items.Clear();
			foreach( WorkflowTaskableGroup grp in Engine.GetTaskableGroups() )
				CSV_AllowedGroups.Items.Add( new ListItem( grp.GroupName, grp.GroupID.ToString() ));
		}
	}
	#endregion
	#region Navigation
	/// <summary>
	/// Returns to process editor
	/// </summary>
	protected void GoBack()
	{
		string url = String.Format(
			"ui_usertask.aspx?HandlerID={0}&DefinitionID={1}&ProcessID={2}&TaskingID={3}",
			this.HandlerID,
			this.DefinitionID,
			this.ProcessID,
			this.TaskingID);
		
		Response.Redirect(url, true);
	}
	/// <summary>
	/// Navigate forward to editor for the exit path
	/// </summary>
	protected void GoForward( int TaskExitPathID )
	{
		string url = String.Format(
			"ui_exitpath.aspx?HandlerID={0}&DefinitionID={1}&ProcessID={2}&TaskingID={3}&TaskExitPathID={4}",
			this.HandlerID,
			this.DefinitionID,
			this.ProcessID,
			this.TaskingID,
			TaskExitPathID);

		Response.Redirect( url , true );
	}
	#endregion
	#region Save/Cancel handlers
	/// <summary>
	/// User cancelled the save, go back to previous page
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancel_Click(object sender, EventArgs e)
	{
		GoBack();
	}
	/// <summary>
	/// User clicked the save button, store the data
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnSave_Click(object sender, EventArgs e)
	{
		if (!Page.IsValid) return;

		TaskExitPath t = new TaskExitPath();
		t.FK_TaskingID = this.TaskingID;
		t.Caption = this.Caption.Text;
		t.uiTaskMethod = this.uiTaskMethod.SelectedValue;
		t.CSV_AllowedGroups = ControlUtil.GetListSelectedCSV(CSV_AllowedGroups.Items);

		TaskExitPaths teps = new TaskExitPaths();
		int newID = teps.Save( t );

		Tasking pt = t.ParentTasking;
		Process pp = pt.ParentProcess;

		QuickAudit.RaiseInformation(
			new AuditUser(),
			"Flux", "Workflow Manager",
			"created exit path ({0}) for tasking ({1}) in process ({2})",
			t.Caption,
			pt.TaskCaption,
			pp.ProcessName);

		GoForward( newID );
	}
	#endregion
}
